home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / lib2to3 / patcomp.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  6KB  |  185 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Pattern compiler.
  5.  
  6. The grammer is taken from PatternGrammar.txt.
  7.  
  8. The compiler compiles a pattern to a pytree.*Pattern instance.
  9. '''
  10. __author__ = 'Guido van Rossum <guido@python.org>'
  11. import os
  12. from pgen2 import driver
  13. from pgen2 import literals
  14. from pgen2 import token
  15. from pgen2 import tokenize
  16. from  import pytree
  17. from  import pygram
  18. _PATTERN_GRAMMAR_FILE = os.path.join(os.path.dirname(__file__), 'PatternGrammar.txt')
  19.  
  20. def tokenize_wrapper(input):
  21.     '''Tokenizes a string suppressing significant whitespace.'''
  22.     skip = (token.NEWLINE, token.INDENT, token.DEDENT)
  23.     tokens = tokenize.generate_tokens(driver.generate_lines(input).next)
  24.     for quintuple in tokens:
  25.         (type, value, start, end, line_text) = quintuple
  26.         if type not in skip:
  27.             yield quintuple
  28.             continue
  29.     
  30.  
  31.  
  32. class PatternCompiler(object):
  33.     
  34.     def __init__(self, grammar_file = _PATTERN_GRAMMAR_FILE):
  35.         '''Initializer.
  36.  
  37.         Takes an optional alternative filename for the pattern grammar.
  38.         '''
  39.         self.grammar = driver.load_grammar(grammar_file)
  40.         self.syms = pygram.Symbols(self.grammar)
  41.         self.pygrammar = pygram.python_grammar
  42.         self.pysyms = pygram.python_symbols
  43.         self.driver = driver.Driver(self.grammar, convert = pattern_convert)
  44.  
  45.     
  46.     def compile_pattern(self, input, debug = False):
  47.         '''Compiles a pattern string to a nested pytree.*Pattern object.'''
  48.         tokens = tokenize_wrapper(input)
  49.         root = self.driver.parse_tokens(tokens, debug = debug)
  50.         return self.compile_node(root)
  51.  
  52.     
  53.     def compile_node(self, node):
  54.         '''Compiles a node, recursively.
  55.  
  56.         This is one big switch on the node type.
  57.         '''
  58.         if node.type == self.syms.Matcher:
  59.             node = node.children[0]
  60.         
  61.         if node.type == self.syms.Alternatives:
  62.             alts = [ self.compile_node(ch) for ch in node.children[::2] ]
  63.             if len(alts) == 1:
  64.                 return alts[0]
  65.             p = []([ [
  66.                 a] for a in alts ], min = 1, max = 1)
  67.             return p.optimize()
  68.         if node.type == self.syms.Alternative:
  69.             units = [ self.compile_node(ch) for ch in node.children ]
  70.             if len(units) == 1:
  71.                 return units[0]
  72.             p = pytree.WildcardPattern([
  73.                 units], min = 1, max = 1)
  74.             return p.optimize()
  75.         if node.type == self.syms.NegatedUnit:
  76.             pattern = self.compile_basic(node.children[1:])
  77.             p = pytree.NegatedPattern(pattern)
  78.             return p.optimize()
  79.         if not node.type == self.syms.Unit:
  80.             raise AssertionError
  81.         name = None
  82.         nodes = node.children
  83.         repeat = None
  84.         pattern = self.compile_basic(nodes, repeat)
  85.         if repeat is not None:
  86.             if not repeat.type == self.syms.Repeater:
  87.                 raise AssertionError
  88.             children = repeat.children
  89.             child = children[0]
  90.             if child.type == token.STAR:
  91.                 min = 0
  92.                 max = pytree.HUGE
  93.             elif child.type == token.PLUS:
  94.                 min = 1
  95.                 max = pytree.HUGE
  96.             elif child.type == token.LBRACE:
  97.                 if not children[-1].type == token.RBRACE:
  98.                     raise AssertionError
  99.                 if not len(children) in (3, 5):
  100.                     raise AssertionError
  101.                 if len(children) == 5:
  102.                     max = self.get_int(children[3])
  103.                 
  104.             elif not False:
  105.                 raise AssertionError
  106.             len(children) in (3, 5)
  107.             if min != 1 or max != 1:
  108.                 pattern = pattern.optimize()
  109.                 pattern = pytree.WildcardPattern([
  110.                     [
  111.                         pattern]], min = min, max = max)
  112.             
  113.         
  114.         if name is not None:
  115.             pattern.name = name
  116.         
  117.         return pattern.optimize()
  118.  
  119.     
  120.     def compile_basic(self, nodes, repeat = None):
  121.         if not len(nodes) >= 1:
  122.             raise AssertionError
  123.         node = nodes[0]
  124.         if node.type == token.STRING:
  125.             value = literals.evalString(node.value)
  126.             return pytree.LeafPattern(content = value)
  127.         if node.type == token.NAME:
  128.             value = node.value
  129.             if value.isupper():
  130.                 if value not in TOKEN_MAP:
  131.                     raise SyntaxError('Invalid token: %r' % value)
  132.                 value not in TOKEN_MAP
  133.                 return pytree.LeafPattern(TOKEN_MAP[value])
  134.             if value == 'any':
  135.                 type = None
  136.             elif not value.startswith('_'):
  137.                 type = getattr(self.pysyms, value, None)
  138.                 if type is None:
  139.                     raise SyntaxError('Invalid symbol: %r' % value)
  140.                 type is None
  141.             
  142.             if nodes[1:]:
  143.                 content = [
  144.                     self.compile_node(nodes[1].children[1])]
  145.             else:
  146.                 content = None
  147.             return pytree.NodePattern(type, content)
  148.         node.type == token.NAME
  149.         if node.value == '(':
  150.             return self.compile_node(nodes[1])
  151.         if node.value == '[':
  152.             if not repeat is None:
  153.                 raise AssertionError
  154.             subpattern = self.compile_node(nodes[1])
  155.             return pytree.WildcardPattern([
  156.                 [
  157.                     subpattern]], min = 0, max = 1)
  158.         if not False:
  159.             raise AssertionError, node
  160.  
  161.     
  162.     def get_int(self, node):
  163.         if not node.type == token.NUMBER:
  164.             raise AssertionError
  165.         return int(node.value)
  166.  
  167.  
  168. TOKEN_MAP = {
  169.     'NAME': token.NAME,
  170.     'STRING': token.STRING,
  171.     'NUMBER': token.NUMBER,
  172.     'TOKEN': None }
  173.  
  174. def pattern_convert(grammar, raw_node_info):
  175.     '''Converts raw node information to a Node or Leaf instance.'''
  176.     (type, value, context, children) = raw_node_info
  177.     if children or type in grammar.number2symbol:
  178.         return pytree.Node(type, children, context = context)
  179.     return pytree.Leaf(type, value, context = context)
  180.  
  181.  
  182. def compile_pattern(pattern):
  183.     return PatternCompiler().compile_pattern(pattern)
  184.  
  185.